package com.example.taobaounion.view.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Paint;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.taobaounion.R;
import com.example.taobaounion.model.domain.ILinearItemInfo;
import com.example.taobaounion.utils.LogUtils;
import com.example.taobaounion.utils.UrlUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class HomePageContentAdapter extends RecyclerView.Adapter<HomePageContentAdapter.InnerHolder> {
    public static final String TAG = "HomePageContentAdapter";
    private List<ILinearItemInfo> data = new ArrayList<>();

    private int time = 0;
    private int time2 = 0;
    private onListenerItemClickListener mListener;

    @NonNull
    @Override
    public HomePageContentAdapter.InnerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        time++;
        LogUtils.d(TAG, "onCreateViewHolder ..." + time);
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_linear_goos_content, parent, false);
        return new InnerHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull HomePageContentAdapter.InnerHolder holder, int position) {
        time2++;
        LogUtils.d(TAG, "onBindViewHolder ..." + time2);

        // set data
        ILinearItemInfo dataBean = data.get(position);
        holder.setData(dataBean);

        holder.itemView.setOnClickListener(v -> {
            if (mListener != null) {
                ILinearItemInfo item = data.get(position);

                mListener.onItemClick(item);
            }
        });
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public void setDate(List<? extends ILinearItemInfo> contentList) {
        data.clear();
        data.addAll(contentList);
        notifyDataSetChanged();
    }

    public void addData(List<? extends ILinearItemInfo> contents) {

        int olderSize = data.size();

        data.addAll(contents);
        // update UI
        notifyItemRangeChanged(olderSize, contents.size());

    }

    public static class InnerHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.goods_cover)
        public ImageView mCover;

        @BindView(R.id.goods_title)
        public TextView title;

        @BindView(R.id.goods_off_prise)
        public TextView offPriseTv;

        @BindView(R.id.goods_after_off_prise)
        public TextView finalPriceTv;

        @BindView(R.id.goods_original_prise)
        public TextView originalPriseTv;

        @BindView(R.id.goods_sell_count)
        public TextView sellCountTv;

        InnerHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        @SuppressLint("DefaultLocale")
        void setData(ILinearItemInfo dataBean) {
            title.setText(dataBean.getTitle());


            Context context = itemView.getContext();

//            Log.d(TAG, "url -->" + dataBean.getPict_url());
            String finalPrise = dataBean.getFinalPrice();
            long couponAmount = dataBean.getCouponAmount();

//            LogUtils.d(TAG, "final price -->" + finalPrise);

            float resultPrise = Float.parseFloat(finalPrise) - couponAmount;

//            LogUtils.d(TAG, "result price -->" + resultPrise);

            if (dataBean.getCover() == null || TextUtils.isEmpty(dataBean.getCover())) {
                mCover.setImageResource(R.mipmap.ic_launcher);
            } else {
                Glide.with(context).load(UrlUtils.getTicketUrl(dataBean.getCover())).into(mCover);
            }


//            LogUtils.d(TAG, "result price -->" + resultPrise);

            finalPriceTv.setText(String.format("%.2f", resultPrise));
            offPriseTv.setText(String.format(context.getString(R.string.text_goods_off_prise), couponAmount));
            originalPriseTv.setText(String.format(context.getString(R.string.text_goods_original_prise), finalPrise));
            // Add the mid line at the TextView
            originalPriseTv.setPaintFlags(Paint.ANTI_ALIAS_FLAG);
            // show how much people have buy the good
            sellCountTv.setText(String.format(context.getString(R.string.text_goods_sell_count), dataBean.getVolume()));
        }
    }

    public void setOnListenerItemClickListener(onListenerItemClickListener clickListener) {
        mListener = clickListener;
    }

    public interface onListenerItemClickListener {
        void onItemClick(ILinearItemInfo dataBean);
    }
}
